package dukeCE.insightTracker;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;

import dukeCE.insightTracker.Classes.commentListAdapter;
import dukeCE.insightTracker.Classes.commentListItem;
import dukeCE.insightTracker.Classes.Topic;
import dukeCE.insightTracker.Classes.commentList;
import dukeCE.insightTracker.Classes.topicContent;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class content_activity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_activity);
        
        //getting the topicID from the home activity
        Bundle extras = getIntent().getExtras();
        int passingTopicID = extras.getInt("topicID");
        
        if(!validAccess())
        {
        //Check for valid access to this activity---
        	/* Check for topicID, loggedIn or not, internet access availability...
        	 * 
        	 * 											*/
        }
        else //feed the data 
        {
        	String serverAddress = "http://kevin2011.byethost3.com/adr_ch_2.php"; //This will be pulled from a global preference file
            String exampleJsonInput=null;
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(serverAddress);
            try {
                // Append httpPost data into httpPost
            		//Send topicID to server and get the queried DB result
            	
                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                //Get the data from response
            } catch (ClientProtocolException e) {
            	exampleJsonInput="Client error";
            } catch (IOException e) {
            	exampleJsonInput = "IO error";
            }
            
     //Assign data into the views
           	//Topic and commentList are created from JSON data
            
            //Assigning variables

                       
            Topic currentTopic = new Topic(0, exampleJsonInput, exampleJsonInput, null); //Getting from previous intent
            
            commentList currentCommentList = new commentList();
            topicContent currentTopicContent = new topicContent(currentTopic, currentCommentList);
            
            String topicName = currentTopic.getName();
            String topicDescription =currentTopic.getDes(); 
            String topicUsername = currentTopic.getUsername();
            int topicVotingData = currentTopic.getVotingData();
            String topicImage = null; //adding later
            float topicMapCoordinates[] = null;
           //-----------------End assigning variables-------------------
            
            
            //test data   
            topicDescription = "Current topic ID:  "+ passingTopicID + "\nTest data: Lorem ipsum dolor sit amet, consectetur adipiscing elit. In auctor varius ligula quis imperdiet. Cras at arcu eget ligula lacinia aliquam. Donec ultricies neque eu nisl elementum sed luctus leo porta. Nunc gravida molestie sodales. Nam sit amet lacus a odio dictum dignissim. Pellentesque nec tincidunt urna. Praesent mattis dignissim ante, eu placerat leo placerat vel. Curabitur nec consequat nisl. Donec dapibus ante id enim faucibus ut scelerisque risus sollicitudin. Vestibulum ipsum sapien, molestie vel dignissim eleifend, ultricies vitae libero. Nullam sapien libero, posuere non ornare sed, tristique non nisi. Proin eu nunc sapien, vitae euismod neque. Fusce scelerisque purus sit amet leo accumsan scelerisque varius ligula interdum. Maecenas non mauris vitae lacus vehicula pulvinar sed quis dui. Nullam ultricies mollis tortor vel dictum. Phasellus tincidunt dui id odio sodales tincidunt. Curabitur malesuada dapibus neque, non malesuada augue tempus at. Integer et nulla et arcu sollicitudin semper. ";       
            
            
            exampleJsonInput = "{\"commentCount\":5,\"commentList\":[{\"" +
            		"commentId\":1,\"topicId\":1,\"username\":\"Json \",\"commentDescription\":\"Gson is awsome\",\"votingData\":5},{\"" +
            		"commentId\":1,\"topicId\":1,\"username\":\"Json \",\"commentDescription\":\"Gson is awsome\",\"votingData\":5},{\"" +
            		"commentId\":1,\"topicId\":1,\"username\":\"Json \",\"commentDescription\":\"Gson is awsome\",\"votingData\":5},{\"" +
            		"commentId\":1,\"topicId\":1,\"username\":\"Json \",\"commentDescription\":\"Gson is awsome\",\"votingData\":5},{\"" +
            		"commentId\":1,\"topicId\":6,\"username\":\"More data\",\"commentDescription\":\"This is just a test\",\"votingData\":3}]}";
            
            Gson gson = new Gson();
            currentCommentList = gson.fromJson(exampleJsonInput, commentList.class);
            //End testdata
            
            int commentCount = currentCommentList.getCount();
            ArrayList<commentListItem> items = new ArrayList<commentListItem>();
            
            for(int i=0;i<commentCount;i++)
            	items.add(new commentListItem(currentCommentList.getTopicByOrder(i).getUsername(),currentCommentList.getTopicByOrder(i).getDescription(),currentCommentList.getTopicByOrder(i).getVotingData()));
            
            
       //Assigning data to the views
            //Views of topic (map,image,topicDes..)
            TextView textViewTopicDescription = (TextView) findViewById(R.id.textViewTopicDescription);
            TextView textViewVotingData = (TextView) findViewById(R.id.textViewVotingData);
            TextView textViewTopicUsername = (TextView) findViewById(R.id.textViewTopicUsername);
            
            textViewTopicDescription.setText(topicDescription);
            textViewTopicUsername.setText("Topic Owner: " + topicUsername);
            textViewVotingData.setText("Vote: " + topicVotingData);
                     //Also update map and image data

            
            //Adapter for commentList
            commentListAdapter itemAdapter = new commentListAdapter(getApplicationContext(), R.layout.comment_list_row, items);        
           
            //update the commentList content
            ListView list = (ListView) findViewById(R.id.commentList);
            list.setAdapter(itemAdapter);
        }
        
    }
    
    private boolean validAccess()
    {
    	return true;
    }
    
    public void backClick(View view) {
    	this.finish();
    }
    public void showMapClick(View view)
    {
        Intent i = new Intent(getApplicationContext(), showMap_activity.class);
        startActivity(i);
    }
    
    public void showPictureClick(View view)
    {
        Intent i = new Intent(getApplicationContext(), showPicture_activity.class);
        startActivity(i);
    }
    
    public void extendContent(View view)
    {
    	ScrollView textbox = (ScrollView) findViewById(R.id.textAreaScroller);
    	textbox.setEnabled(false);
    }
}
